package com.yonyou.crm.bpub.template.appctrl;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.rest.util.*;
import com.yonyou.crm.common.security.util.Md5CodeUtil;
import com.yonyou.crm.sys.modules.constants.ClientType;
import com.yonyou.crm.sys.modules.constants.LayoutType;
import com.yonyou.crm.sys.modules.entity.MtObjTypeEnum;
import com.yonyou.crm.bpub.template.entity.*;
import com.yonyou.crm.bpub.template.rmitf.ITemplatesRmService;

@RequestMapping("/{version}/*/{module}/templates")
@RestController
public class AppTemplatesCtrl{
	
	@Autowired
	ITemplatesRmService service;
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public Object getTemplate(@PathVariable("version") String version, @PathVariable("module") String module,HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		Map<String, Object> param = new HashMap<String, Object>();
		String appversion = paramObj.getString("version");
//		String subbnstype = "";
//		if (paramObj.get("subbnstype") != null) {
//			subbnstype = String.valueOf(paramObj.get("subbnstype"));
//		}
//		param.put("module",module);
//		param.put("subbnstype",subbnstype);
		
		param = convertParam(paramObj, module);
		Map<String, Object> list = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();
		
		TemplatesVO result = service.getTemplate(param);
		
		data.put("headerlist", result.getHeaderlist());
		data.put("bodylist", result.getBodylist());
		data.put("triggerlist", result.getTriggerlist());
		data.put("filterlist", result.getFilterlist());
		
		String hashCodeVersion = Md5CodeUtil.createMd5CodeForMap(data);
		
		if (StringUtils.isBlank(appversion) || (!appversion.equals(hashCodeVersion))) {
			list = data;
		}
		
		list.put("version", hashCodeVersion);

		return ResponseUtil.getObject(list);
	}
	
	public Map<String, Object> convertParam(JSONObject paramObj, String module){
		Map<String, Object> param = new HashMap<String, Object>();
		Long moduleId = null;
		switch (module) {
		case "accounts":
			moduleId = MtObjTypeEnum.CUSTOMER.getValue();
			break;
		case "opportunities":
			break;
		case "contacts":
			break;
		case "leads":
			break;
		default:
		}
		Integer layoutType = LayoutType.EDIT.getIndex();
		Integer clientType = ClientType.MOBILE.getIndex();
		String subbnstype = "";
		if (paramObj.get("subbnstype") != null) {
			subbnstype = String.valueOf(paramObj.get("subbnstype"));
		}
		if (moduleId != null) {
			param.put("module", moduleId);
		} else {
			param.put("module", module);
		}
		param.put("subbnstype", subbnstype);
		param.put("layouttype", layoutType);
		param.put("clienttype", clientType);
		return param;
	}

}